<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="crontab">
      Crontab中文表达式解析
     </h1>
     <hr/>
     <h2 id="_1">
      简介
     </h2>
     <p>
      最近工作中在使用调度框架，经常和定时表达式打交道，并且有查看表达式中文解释的需求，于是在网上搜集资料和自己进行一定的修改，写了一个Crontab表达式解析的工具类
     </p>
     <h2 id="_2">
      详解
     </h2>
     <p>
      这个没啥好解释，看资料，自己试着动手写写，看看运行时间示例是否和自己想象的一样，就差不多了
     </p>
     <p>
      不多说，直接上代码：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">import</span> <span class="nn">org.springframework.scheduling.support.CronSequenceGenerator</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">java.text.SimpleDateFormat</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.util.ArrayList</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.util.Date</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.util.List</span><span class="p">;</span>

<span class="cm">/**</span>
<span class="cm"> * crontab 工具类</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">CrontabUtils</span> <span class="p">{</span>

    <span class="cm">/**</span>
<span class="cm">     * 需要展示的表达式时间条数</span>
<span class="cm">     */</span>
    <span class="kd">private</span> <span class="kd">final</span> <span class="kd">static</span> <span class="kt">int</span> <span class="n">size</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">common</span> <span class="o">=</span> <span class="s">"*"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">questionMark</span> <span class="o">=</span> <span class="s">"?"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">hyphen</span> <span class="o">=</span> <span class="s">"-"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">slash</span> <span class="o">=</span> <span class="s">"/"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">comma</span> <span class="o">=</span> <span class="s">","</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">second</span> <span class="o">=</span> <span class="s">"秒"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">minute</span> <span class="o">=</span> <span class="s">"分"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">hour</span> <span class="o">=</span> <span class="s">"时"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">day</span> <span class="o">=</span> <span class="s">"日"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">week</span> <span class="o">=</span> <span class="s">"周"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">month</span> <span class="o">=</span> <span class="s">"月"</span><span class="p">;</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="n">String</span> <span class="nf">parse</span><span class="p">(</span><span class="n">String</span> <span class="n">crontab</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">try</span> <span class="p">{</span>
            <span class="n">StringBuilder</span> <span class="n">result</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringBuilder</span><span class="p">(</span><span class="s">"表达式："</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">crontab</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="s">"\n"</span><span class="p">);</span>
            <span class="n">result</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="n">translateToChinese</span><span class="p">(</span><span class="n">crontab</span><span class="p">)).</span><span class="na">append</span><span class="p">(</span><span class="s">"\n"</span><span class="p">);</span>
            <span class="n">result</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"\n"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="s">"表达式执行时间列表示例："</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="s">"\n"</span><span class="p">);</span>
            <span class="k">for</span> <span class="p">(</span><span class="n">String</span> <span class="n">exampleTime</span> <span class="p">:</span> <span class="n">examples</span><span class="p">(</span><span class="n">crontab</span><span class="p">))</span> <span class="p">{</span>
                <span class="n">result</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="n">exampleTime</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="s">"\n"</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="k">return</span> <span class="n">result</span><span class="p">.</span><span class="na">toString</span><span class="p">();</span>
        <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="s">"无Crontab表达式"</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="nf">examples</span><span class="p">(</span><span class="n">String</span> <span class="n">crontab</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">final</span> <span class="n">CronSequenceGenerator</span> <span class="n">generator</span> <span class="o">=</span> <span class="k">new</span> <span class="n">CronSequenceGenerator</span><span class="p">(</span><span class="n">crontab</span><span class="p">);</span>
        <span class="n">Date</span> <span class="n">date</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Date</span><span class="p">();</span>
        <span class="n">SimpleDateFormat</span> <span class="n">sdf</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SimpleDateFormat</span><span class="p">(</span><span class="s">"yyyy-MM-dd HH:mm:ss"</span><span class="p">);</span>
        <span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">res</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;</span><span class="p">(</span><span class="n">size</span><span class="p">);</span>
        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">size</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">date</span> <span class="o">=</span> <span class="n">generator</span><span class="p">.</span><span class="na">next</span><span class="p">(</span><span class="n">date</span><span class="p">);</span>
            <span class="n">res</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">sdf</span><span class="p">.</span><span class="na">format</span><span class="p">(</span><span class="n">date</span><span class="p">));</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">res</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="n">String</span> <span class="nf">translateToChinese</span><span class="p">(</span><span class="n">String</span> <span class="n">cronExp</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">String</span><span class="o">[]</span> <span class="n">tmpCorns</span> <span class="o">=</span> <span class="n">cronExp</span><span class="p">.</span><span class="na">split</span><span class="p">(</span><span class="s">" "</span><span class="p">);</span>
        <span class="n">StringBuffer</span> <span class="n">sBuffer</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringBuffer</span><span class="p">();</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">tmpCorns</span><span class="p">.</span><span class="na">length</span> <span class="o">!=</span> <span class="mi">6</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">throw</span> <span class="k">new</span> <span class="n">RuntimeException</span><span class="p">(</span><span class="s">"请补全表达式,必须标准的cron表达式才能解析"</span><span class="p">);</span>
        <span class="p">}</span>
        <span class="c1">// 解析月</span>
        <span class="n">descMonth</span><span class="p">(</span><span class="n">tmpCorns</span><span class="o">[</span><span class="mi">4</span><span class="o">]</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">);</span>
        <span class="c1">// 解析周</span>
        <span class="n">descWeek</span><span class="p">(</span><span class="n">tmpCorns</span><span class="o">[</span><span class="mi">5</span><span class="o">]</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">);</span>
        <span class="c1">// 解析日</span>
        <span class="n">descDay</span><span class="p">(</span><span class="n">tmpCorns</span><span class="o">[</span><span class="mi">3</span><span class="o">]</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">);</span>
        <span class="c1">// 解析时</span>
        <span class="n">descHour</span><span class="p">(</span><span class="n">tmpCorns</span><span class="o">[</span><span class="mi">2</span><span class="o">]</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">);</span>
        <span class="c1">// 解析分</span>
        <span class="n">descMinute</span><span class="p">(</span><span class="n">tmpCorns</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">);</span>
        <span class="c1">// 解析秒</span>
        <span class="n">descSecond</span><span class="p">(</span><span class="n">tmpCorns</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">sBuffer</span><span class="p">.</span><span class="na">toString</span><span class="p">();</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">descSecond</span><span class="p">(</span><span class="n">String</span> <span class="n">s</span><span class="p">,</span> <span class="n">StringBuffer</span> <span class="n">sBuffer</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">desc</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">,</span> <span class="n">second</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">descMinute</span><span class="p">(</span><span class="n">String</span> <span class="n">s</span><span class="p">,</span> <span class="n">StringBuffer</span> <span class="n">sBuffer</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">desc</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">,</span> <span class="n">minute</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">descHour</span><span class="p">(</span><span class="n">String</span> <span class="n">s</span><span class="p">,</span> <span class="n">StringBuffer</span> <span class="n">sBuffer</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">desc</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">,</span> <span class="n">hour</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">descDay</span><span class="p">(</span><span class="n">String</span> <span class="n">s</span><span class="p">,</span> <span class="n">StringBuffer</span> <span class="n">sBuffer</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">desc</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">,</span> <span class="n">day</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">descWeek</span><span class="p">(</span><span class="n">String</span> <span class="n">s</span><span class="p">,</span> <span class="n">StringBuffer</span> <span class="n">sBuffer</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">desc</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">,</span> <span class="n">week</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">descMonth</span><span class="p">(</span><span class="n">String</span> <span class="n">s</span><span class="p">,</span> <span class="n">StringBuffer</span> <span class="n">sBuffer</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">desc</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">sBuffer</span><span class="p">,</span> <span class="n">month</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">desc</span><span class="p">(</span><span class="n">String</span> <span class="n">s</span><span class="p">,</span> <span class="n">StringBuffer</span> <span class="n">sBuffer</span><span class="p">,</span> <span class="n">String</span> <span class="n">flag</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="s">"1/1"</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">s</span><span class="o">=</span><span class="s">"*"</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="s">"0/0"</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">s</span><span class="o">=</span><span class="s">"0"</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">common</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">flag</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="n">week</span><span class="p">))</span> <span class="p">{</span>
                <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"每"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">turnWeek</span><span class="p">(</span><span class="n">s</span><span class="p">));</span>
            <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"每"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">flag</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">questionMark</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="p">{</span>
            <span class="k">return</span> <span class="p">;</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="na">contains</span><span class="p">(</span><span class="n">comma</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">String</span><span class="o">[]</span> <span class="n">arr</span> <span class="o">=</span> <span class="n">s</span><span class="p">.</span><span class="na">split</span><span class="p">(</span><span class="n">comma</span><span class="p">);</span>
            <span class="k">for</span> <span class="p">(</span><span class="n">String</span> <span class="n">value</span> <span class="p">:</span> <span class="n">arr</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">value</span><span class="p">.</span><span class="na">length</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">flag</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="n">week</span><span class="p">))</span> <span class="p">{</span>
                        <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="n">turnWeek</span><span class="p">(</span><span class="n">value</span><span class="p">)).</span><span class="na">append</span><span class="p">(</span><span class="s">"和"</span><span class="p">);</span>
                    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                        <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"第"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">value</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">flag</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="s">"和"</span><span class="p">);</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">}</span>
            <span class="n">sBuffer</span><span class="p">.</span><span class="na">deleteCharAt</span><span class="p">(</span><span class="n">sBuffer</span><span class="p">.</span><span class="na">length</span><span class="p">()</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
            <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"的"</span><span class="p">);</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="na">contains</span><span class="p">(</span><span class="n">hyphen</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">String</span><span class="o">[]</span> <span class="n">arr</span> <span class="o">=</span> <span class="n">s</span><span class="p">.</span><span class="na">split</span><span class="p">(</span><span class="n">hyphen</span><span class="p">);</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">arr</span><span class="p">.</span><span class="na">length</span><span class="o">!=</span><span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">throw</span> <span class="k">new</span> <span class="n">RuntimeException</span><span class="p">(</span><span class="s">"表达式错误"</span><span class="o">+</span><span class="n">s</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">flag</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="n">week</span><span class="p">))</span> <span class="p">{</span>
                <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="n">turnWeek</span><span class="p">(</span><span class="n">arr</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">)).</span><span class="na">append</span><span class="p">(</span><span class="n">turnWeek</span><span class="p">(</span><span class="n">arr</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">));</span>
            <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"从第"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">arr</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">flag</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="s">"到第"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">arr</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">flag</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="na">contains</span><span class="p">(</span><span class="n">slash</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">String</span><span class="o">[]</span> <span class="n">arr</span> <span class="o">=</span> <span class="n">s</span><span class="p">.</span><span class="na">split</span><span class="p">(</span><span class="n">slash</span><span class="p">);</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">arr</span><span class="p">.</span><span class="na">length</span><span class="o">!=</span><span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">throw</span> <span class="k">new</span> <span class="n">RuntimeException</span><span class="p">(</span><span class="s">"表达式错误"</span><span class="o">+</span><span class="n">s</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">arr</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="n">arr</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">)</span><span class="o">||</span><span class="n">arr</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="s">"0"</span><span class="p">))</span> <span class="p">{</span>
                <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"每"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">arr</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">flag</span><span class="p">);</span>
            <span class="p">}</span><span class="k">else</span> <span class="p">{</span>
                <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"每"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">arr</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">flag</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="s">"的第"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">arr</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">flag</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">flag</span><span class="p">.</span><span class="na">equals</span><span class="p">(</span><span class="n">week</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"每"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">turnWeek</span><span class="p">(</span><span class="n">s</span><span class="p">));</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="n">sBuffer</span><span class="p">.</span><span class="na">append</span><span class="p">(</span><span class="s">"第"</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">s</span><span class="p">).</span><span class="na">append</span><span class="p">(</span><span class="n">flag</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="n">String</span> <span class="nf">turnWeek</span><span class="p">(</span><span class="n">String</span> <span class="n">week</span><span class="p">){</span>
        <span class="k">switch</span> <span class="p">(</span><span class="n">week</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">case</span> <span class="s">"0"</span><span class="p">:</span>
            <span class="k">return</span><span class="s">"周天"</span><span class="p">;</span>
        <span class="k">case</span> <span class="s">"1"</span><span class="p">:</span>
            <span class="k">return</span><span class="s">"周一"</span><span class="p">;</span>
        <span class="k">case</span> <span class="s">"2"</span><span class="p">:</span>
            <span class="k">return</span><span class="s">"周二"</span><span class="p">;</span>
        <span class="k">case</span> <span class="s">"3"</span><span class="p">:</span>
            <span class="k">return</span><span class="s">"周三"</span><span class="p">;</span>
        <span class="k">case</span> <span class="s">"4"</span><span class="p">:</span>
            <span class="k">return</span><span class="s">"周四"</span><span class="p">;</span>
        <span class="k">case</span> <span class="s">"5"</span><span class="p">:</span>
            <span class="k">return</span><span class="s">"周五"</span><span class="p">;</span>
        <span class="k">case</span> <span class="s">"6"</span><span class="p">:</span>
            <span class="k">return</span><span class="s">"周六"</span><span class="p">;</span>
        <span class="k">case</span> <span class="s">"7"</span><span class="p">:</span>
            <span class="k">return</span><span class="s">"周日"</span><span class="p">;</span>
        <span class="k">default</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">null</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">String</span> <span class="n">s</span> <span class="p">:</span> <span class="n">parse</span><span class="p">(</span><span class="s">"0/10 3/5 1-3 * 1 0"</span><span class="p">).</span><span class="na">split</span><span class="p">(</span><span class="s">"\n"</span><span class="p">))</span> <span class="p">{</span>
            <span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
     <p>
      执行的结果如下：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">表达式：</span><span class="mi">0</span><span class="o">/</span><span class="mi">10</span> <span class="mi">3</span><span class="o">/</span><span class="mi">5</span> <span class="mi">1</span><span class="o">-</span><span class="mi">3</span> <span class="o">*</span> <span class="mi">1</span> <span class="mi">0</span>
<span class="err">第</span><span class="mi">1</span><span class="err">月每周天每日从第</span><span class="mi">1</span><span class="err">时到第</span><span class="mi">3</span><span class="err">时每</span><span class="mi">5</span><span class="err">分的第</span><span class="mi">3</span><span class="err">分每</span><span class="mi">10</span><span class="err">秒</span>

<span class="err">表达式执行时间列表示例：</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">03</span><span class="p">:</span><span class="mi">00</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">03</span><span class="p">:</span><span class="mi">10</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">03</span><span class="p">:</span><span class="mi">20</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">03</span><span class="p">:</span><span class="mi">30</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">03</span><span class="p">:</span><span class="mi">40</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">03</span><span class="p">:</span><span class="mi">50</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mi">00</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mi">10</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mi">20</span>
<span class="mi">2022</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">01</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mi">30</span>
</code></pre>
     </div>
     <h2 id="_3">
      参考链接
     </h2>
     <ul>
      <li>
       <a href="https://tool.lu/crontab/">
        Crontab 在线工具
       </a>
      </li>
      <li>
       <a href="https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions">
        New in Spring 5.3: Improved Cron Expressions
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/PiaoMiaoXiaodao/article/details/87982486">
        将cron表达式解析成中文,方便客户理解
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/jimo_lonely/article/details/105418387">
        spring实战代码之解析CRON表达式
       </a>
      </li>
      <li>
       <a href="https://www.cnblogs.com/panie2015/p/5553404.html">
        将定时任务cron 解析成中文
       </a>
      </li>
     </ul>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
